'''
Author: dvlproad
Date: 2022-10-10 18:46:08
LastEditors: dvlproad
LastEditTime: 2023-10-18 18:37:06
Description: 
'''
#!/user/local/bin/python3
#将本文件放在Flutter项目的根目录

import os
import json

# from . import path_util # 为了能使用远程脚本，这里的代码就不写成 from path_util import joinFullPath 了
# 路径拼接(①支持尾部及头部斜杠的处理;②支持尾部拼接../)
def joinFullPath(host_dir, rel_path):
    # 在 Unix 和 Linux 系统中，以斜杠开头的路径被视为绝对路径。所以需要去掉头部结尾的斜杠或者尾部开头的斜杠
    if host_dir.endswith("/"):
        host_dir = host_dir[:-1]
    if rel_path.startswith("/"):
        rel_path = rel_path[1:]
    full_path = os.path.join(host_dir, rel_path)
    full_abspath = os.path.abspath(full_path)

    # print("full_abspath: \033[1;31m{}\033[0m".format(full_abspath))
    return full_abspath

def getEnvValue_params_file_path():
    # return "/Users/qian/Project/CQCI/script-branch-json-file/test/tool_input.json"
    tool_params_file_path = os.getenv('QTOOL_DEAL_PROJECT_PARAMS_FILE_PATH')
    if tool_params_file_path.startswith('~'):
        tool_params_file_path = os.path.expanduser(tool_params_file_path) # 将~扩展为当前用户的home目录
    return tool_params_file_path

def getEnvValue_params_file_data():
    tool_params_file_path = getEnvValue_params_file_path()
    try:
        with open(tool_params_file_path) as f:
            data = json.load(f)
    except FileNotFoundError:
        print(f"{RED}File {tool_params_file_path} not found. {NC}")
        return 1
    except json.JSONDecodeError:
        print(f"{RED}Failed to load JSON data from file {tool_params_file_path} {NC}")
        return 1
    return data

# 获取环境变量的值-项目路径
def getEnvValue_project_dir_path():
    tool_params_file_path = getEnvValue_params_file_path()
    tool_params_file_data = getEnvValue_params_file_data()
    if tool_params_file_data == 1:
        return 1
    project_home_path_rel_this_dir = tool_params_file_data['project_path']['home_path_rel_this_dir']
    project_dir_abspath = joinFullPath(os.path.dirname(tool_params_file_path), project_home_path_rel_this_dir)
    # print("project_dir_abspath: \033[1;31m{}\033[0m".format(project_dir_abspath))
    return project_dir_abspath


# 定义颜色常量
NC='\033[0m' # No Color
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'

tool_params_file_path=getEnvValue_params_file_path()


data = getEnvValue_params_file_data()
if data == 1:
    exit(1)
project_home_path_rel_this_dir = data['project_path']['home_path_rel_this_dir']
project_dir_abspath = joinFullPath(os.path.dirname(tool_params_file_path), project_home_path_rel_this_dir)
# print("project_dir_abspath: \033[1;31m{}\033[0m".format(project_dir_abspath))
if not os.path.exists(project_dir_abspath):
    print(f"{RED}不存在项目绝对路径 {project_dir_abspath}. {NC}")
    exit(1)
print(f"{GREEN}您将要排查的项目绝对路径是 {project_dir_abspath}. {NC}")
projectAbsRootPath= project_dir_abspath


# 打开文件
rel_assetPath=data["project_path"]["other_path_rel_home"]["images"]
rel_codePath=data["project_path"]["other_path_rel_home"]["code"]
assetDir=joinFullPath(projectAbsRootPath, rel_assetPath)
checkInCodeDir=joinFullPath(projectAbsRootPath, rel_codePath)
resultSaveToSheetFilePath=joinFullPath(projectAbsRootPath, "APP资源清单表5.xlsx")
ifUnuseRmoveDirectly = False # 如果未使用是否直接本地删除掉该文件，省去手动一个个去删除



import subprocess
current_script_path = os.path.abspath(__file__) # 当前脚本路径
current_script_dir = os.path.dirname(current_script_path) # 当前脚本所在目录
test_script_path = f"{current_script_dir}/base_remove_unused_resources.py"
codeSuffixs = [".dart", ".java", ".cpp"]
codeSuffixsString = ",".join(codeSuffixs)    # 将 codeSuffixs 转换为字符串形式，以逗号分隔后传递给脚本

# 注意这里要用 python3.9
subprocess.run(["python3.9", test_script_path, "-assetDir", assetDir, "-checkInCodeDir", checkInCodeDir, "-codeSuffixsString", codeSuffixsString, "-resultSaveToSheetFilePath", resultSaveToSheetFilePath])